<template>
  <div class="register-container">
    <el-card class="register-card">
      <template #header>
        <div class="register-header">
          <h2>用户注册</h2>
        </div>
      </template>
      
      <el-form :model="form" :rules="rules" ref="registerForm">
        <el-form-item prop="usertype">
          <el-radio-group v-model="form.usertype">
            <el-radio-button label="employee">员工</el-radio-button>
            <el-radio-button label="customer">客户</el-radio-button>
          </el-radio-group>
        </el-form-item>
        
        <el-form-item prop="username">
          <el-input v-model="form.username" placeholder="请输入账号" prefix-icon="User" />
        </el-form-item>
        
        <el-form-item prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名" prefix-icon="User" />
        </el-form-item>
        
        <el-form-item prop="password">
          <el-input v-model="form.password" placeholder="请输入密码" show-password prefix-icon="Lock" />
        </el-form-item>
        
        <el-form-item prop="confirmPassword">
          <el-input v-model="form.confirmPassword" placeholder="请确认密码" show-password prefix-icon="Lock" />
        </el-form-item>
        
        <template v-if="form.usertype === 'customer'">
          <el-form-item prop="company">
            <el-input v-model="form.company" placeholder="请输入公司名称" prefix-icon="OfficeBuilding" />
          </el-form-item>
          
          <el-form-item prop="phone">
            <el-input v-model="form.phone" placeholder="请输入联系电话" prefix-icon="Phone" />
          </el-form-item>
        </template>
        
        <el-form-item>
          <el-button type="primary" @click="handleRegister" :loading="loading">注册</el-button>
          <el-button @click="$router.push('/login')">返回登录</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { ref, watch } from 'vue'
import { useRouter } from 'vue-router'
import { register } from '@/api/auth'
import { ElMessage } from 'element-plus'

const router = useRouter()

const form = ref({
  usertype: 'customer',
  username: '',
  name: '',
  password: '',
  confirmPassword: '',
  company: '',
  phone: ''
})

const validatePassword = (rule, value, callback) => {
  if (value !== form.value.password) {
    callback(new Error('两次输入密码不一致'))
  } else {
    callback()
  }
}

const rules = {
  usertype: [{ required: true, message: '请选择用户类型', trigger: 'change' }],
  username: [{ required: true, message: '请输入账号', trigger: 'blur' }],
  name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
  confirmPassword: [
    { required: true, message: '请确认密码', trigger: 'blur' },
    { validator: validatePassword, trigger: 'blur' }
  ],
  company: [{ required: true, message: '请输入公司名称', trigger: 'blur' }],
  phone: [{ required: true, message: '请输入联系电话', trigger: 'blur' }]
}

const registerForm = ref(null)
const loading = ref(false)

const handleRegister = () => {
  registerForm.value.validate(async (valid) => {
    if (!valid) return
    
    loading.value = true
    try {
      const data = {
        username: form.value.username,
        name: form.value.name,
        password: form.value.password,
        user_type: form.value.usertype
      }
      
      if (form.value.usertype === 'customer') {
        data.company = form.value.company
        data.phone = form.value.phone
      }
      
      await register(data)
      ElMessage.success('注册成功，等待管理员审核')
      router.push('/login')
    } catch (error) {
      console.error(error)
    } finally {
      loading.value = false
    }
  })
}
</script>

<style scoped>
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f5f7fa;
}

.register-card {
  width: 450px;
}

.register-header {
  text-align: center;
}
</style>